#pragma once
#ifndef _CLogFile_H_
#define _CLogFile_H_

#define		DEF_LOG_FILE_NAME		"ViewImageAppLog.txt"
// Design CLogFile is like Singleton class
// CLogFile: Log all activities in Log file  
class CLogFile
{
	FILE*	m_pLogFile;
	long	m_lTruncate;
	CRITICAL_SECTION	m_cs;
	// Declare the global static class
	static CLogFile *m_cLogFile;
	CLogFile()
	{
		m_pLogFile = 0;
	}
public:

	
	~CLogFile(void);
	// Get the instance of CLogFile : only one CLogFile
	static CLogFile *GetCLogFileInstance()
	{
		if ( m_cLogFile == 0 )
		{
			m_cLogFile = new CLogFile;
		}
		return m_cLogFile;
	}
	BOOL WriteLogToFile(char *szLine, ...)
	{
		if (!m_pLogFile)
			return FALSE;

		EnterCriticalSection(&m_cs);
		//write the formated log string to szLog
		//TCHAR	szLog[256];
	//	char    szLog[256];
		/*
		va_list argList;
		va_start( argList, pszFormat );
		vsprintf( szLog, pszFormat, argList );
		va_end( argList );
*/
		//Trancate if the file grow too large
//		long	lLength = ftell(m_pLogFile);
//		if (lLength > m_lTruncate)
//			rewind(m_pLogFile);

		////Get current time
		//SYSTEMTIME	time;
		//::GetLocalTime(&time);
		//TCHAR	szLine[256];
		//char szLine[256];
		// ido kijelzessel
//		sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n", 
//			time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
//			szLog);
		
		/*sprintf(szLine, "%04d/%02d/%02d %02d:%02d:%02d:%03d \t%s\n", 
			time.wYear, time.wMonth, time.wDay,
			time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
			szLog);*/
		
		fputs(szLine, m_pLogFile);

		fflush(m_pLogFile);

		LeaveCriticalSection(&m_cs);
		return TRUE;
	}
private:
	void OpenFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);
	void ChangeFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);
	void CloseFile();

};

#endif

////	CLogFile, a debug log file wrapper
//class CLogFile
//{
//public:
//	CreateDirectories(CString filename);
//	//	Constructor, open the logfile
//	CLogFile();
//
//	//	Destructor, close if logfile if opened
//	~CLogFile();
//
//	void OpenFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);
//	void ChangeFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);
//	void CloseFile();
//
//	//	Write log info into the logfile, with printf like parameters support
//	void Write(LPCTSTR pszFormat, ...)
//	{
//		void OpenFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);
//	void ChangeFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);
//	void CloseFile();
//
//	//	Write log info into the logfile, with printf like parameters support
//	void Write(LPCTSTR pszFormat, ...)
//	{
//
//		if (!m_pLogFile)
//			return;
//
//		EnterCriticalSection(&m_cs);
//		//write the formated log string to szLog
//		TCHAR	szLog[256];
//		va_list argList;
//		va_start( argList, pszFormat );
//		vsprintf( szLog, pszFormat, argList );
//		va_end( argList );
//
//		//Trancate if the file grow too large
////		long	lLength = ftell(m_pLogFile);
////		if (lLength > m_lTruncate)
////			rewind(m_pLogFile);
//
//		//Get current time
//		SYSTEMTIME	time;
//		::GetLocalTime(&time);
//		TCHAR	szLine[256];
//		
//		// ido kijelzessel
////		sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n", 
////			time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
////			szLog);
//		
//		sprintf(szLine, "%04d/%02d/%02d %02d:%02d:%02d:%03d \t%s\n", 
//			time.wYear, time.wMonth, time.wDay,
//			time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
//			szLog);
//		
//		fputs(szLine, m_pLogFile);
//
//		fflush(m_pLogFile);
//
//		LeaveCriticalSection(&m_cs);
//	}
//
//private:
//	FILE*	m_pLogFile;
//	long	m_lTruncate;
//	CRITICAL_SECTION	m_cs;
//
//	CString	m_filename;
//#ifdef _DEBUG_LOG
//		if (!m_pLogFile)
//			return;
//
//		EnterCriticalSection(&m_cs);
//		//write the formated log string to szLog
//		TCHAR	szLog[256];
//		va_list argList;
//		va_start( argList, pszFormat );
//		vsprintf( szLog, pszFormat, argList );
//		va_end( argList );
//
//		//Trancate if the file grow too large
////		long	lLength = ftell(m_pLogFile);
////		if (lLength > m_lTruncate)
////			rewind(m_pLogFile);
//
//		//Get current time
//		SYSTEMTIME	time;
//		::GetLocalTime(&time);
//		TCHAR	szLine[256];
//		
//		// ido kijelzessel
////		sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n", 
////			time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
////			szLog);
//		
//		sprintf(szLine, "%04d/%02d/%02d %02d:%02d:%02d:%03d \t%s\n", 
//			time.wYear, time.wMonth, time.wDay,
//			time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
//			szLog);
//		
//		fputs(szLine, m_pLogFile);
//
//		fflush(m_pLogFile);
//
//		LeaveCriticalSection(&m_cs);
//#endif
//	}
//
//private:
//	FILE*	m_pLogFile;
//	long	m_lTruncate;
//	CRITICAL_SECTION	m_cs;
//
//	CString	m_filename;
//};
//
//#endif //_ATA_LOGFILE_